package pers.supeijin.devtools.converter.wx2vue;

import com.intellij.lang.xml.XMLLanguage;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiFileFactory;
import com.intellij.psi.codeStyle.CodeStyleManager;
import com.intellij.psi.util.PsiUtilBase;
import com.intellij.psi.xml.XmlTag;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import pers.supeijin.devtools.converter.base.AbsConverterHelper;
import pers.supeijin.devtools.converter.entity.WexinFileInfo;
import pers.supeijin.devtools.converter.wx2vue.converter.ScriptConverter;
import pers.supeijin.devtools.converter.wx2vue.converter.StyleConverter;
import pers.supeijin.devtools.converter.wx2vue.converter.TemplateConverter;
import pers.supeijin.devtools.utils.GeneralContext;

/**
 * @author bluefox
 * @date 2020/2/21.
 */

public class WeiXin2VueHelper extends AbsConverterHelper<WexinFileInfo> {

	public WeiXin2VueHelper(GeneralContext context, VirtualFile selectedVirtualFile) {
		super(context, selectedVirtualFile);
	}

	@Override
	public WexinFileInfo getConverterItem(String fileNameWithoutExtension) {
		return new WexinFileInfo(fileNameWithoutExtension);
	}

	@Override
	public void transformVirtualFile(List<WexinFileInfo> infoList) {
		if (infoList != null && infoList.size() > 0) {
			//开始转换信息
			infoList.forEach(fileInfo -> {
				String fileName = fileInfo.getJsFile().getName().replace(".js", ".vue");
				final XmlTag templateTag = getTemplateXmlTag(fileInfo);
				final XmlTag styleTag = getStyleXmlTag(fileInfo);
				final XmlTag scriptTag = getScriptXmlTag(fileInfo);
				addVueFile(fileName, templateTag, styleTag, scriptTag);
				openFile(fileName);
			});
		}
	}

	/**
	 * 获取模版标签
	 *
	 * @param fileInfo
	 * @return
	 */
	private XmlTag getTemplateXmlTag(WexinFileInfo fileInfo) {
		TemplateConverter templateConverter = new TemplateConverter(mGeneralContext, fileInfo.getWxmlFile());
		return templateConverter.getTemplateTag();
	}


	/**
	 * 获取js模版标签
	 *
	 * @param fileInfo
	 * @return
	 */
	private XmlTag getScriptXmlTag(WexinFileInfo fileInfo) {
		ScriptConverter scriptConverter = new ScriptConverter(mGeneralContext, fileInfo.getJsFile());
		return scriptConverter.getScriptTag();
	}

	/**
	 * 获取样式 标签
	 *
	 * @param fileInfo
	 * @return
	 */
	private XmlTag getStyleXmlTag(WexinFileInfo fileInfo) {
		VirtualFile styleFile = fileInfo.getLessFile() != null ? fileInfo.getLessFile() : fileInfo.getWxssFile();
		StyleConverter styleConverter = new StyleConverter(mGeneralContext, styleFile);
		return styleConverter.getStyleTag();
	}

	/**
	 * 追加vue 模版文件
	 *
	 * @param fileName
	 * @param templateTag
	 * @param styleTag
	 * @param scriptTag
	 * @return
	 */
	@NotNull
	private void addVueFile(String fileName, XmlTag templateTag, XmlTag styleTag, XmlTag scriptTag) {
		StringBuilder sb = new StringBuilder();
		sb.append("\t\n")
				.append(templateTag.getText())
				.append("\t\n")
				.append(scriptTag.getText())
				.append("\t\n")
				.append(styleTag.getText());
		PsiFileFactory fileFactory = PsiFileFactory.getInstance(mProject);
		PsiFile htmlPsiFile = fileFactory.createFileFromText(fileName, XMLLanguage.INSTANCE, sb.toString());
		mPsiDirectory.add(htmlPsiFile);
	}

	/**
	 * 打开文件
	 * @param fileName 文件名称
	 */
	private void openFile(String fileName) {
		Timer timer = new Timer();
		timer.schedule(new TimerTask() {
			public void run() {
				WriteCommandAction.runWriteCommandAction(mProject, () -> {
					VirtualFile virtualFile = mGeneralContext.getVirtualFile();
					String filePath = virtualFile.getPath() + File.separator + fileName;
					VirtualFile realVirtualFile = mGeneralContext.getVirtualFileSystem().findFileByPath(filePath);
					PsiFile psiFile = PsiUtilBase.getPsiFile(mGeneralContext.getProject(), realVirtualFile);
					psiFile.navigate(true);
					CodeStyleManager.getInstance(mProject).reformat(psiFile);
				});
			}
		}, 1500);
	}
}
